import numpy as np  # useful for many scientific computing in Python
import pandas as pd # primary data structure library
import folium
from folium import FeatureGroup, LayerControl, Map, Marker, features
import folium.plugins as plugins 
from folium.plugins import FloatImage, HeatMap, MarkerCluster, FastMarkerCluster, MeasureControl, MousePosition
from folium.features import DivIcon


loc = 'Traffics polyline-Continental USA Tour traversal Map in a non-overlapping manner'
title_html = '''
             <h2 align="center" style="font-size:18px"><b>{}</b></h2>
             '''.format(loc)   
# Let us define a map with the US in the center and a reasonable Zool level with an Open StreetMap 
ContinentalUSTourMap = folium.Map(location=[40.0, -100], zoom_start=4, tiles="OpenStreetMap")

# Let us define Locations of the states to traverse in sequence. 
# These states are as per US Map. We can also use an algorithm on Latitude & Longitude for this as well
#

Locations = ['WA', 'OR', 'CA','NV','AZ','UT','ID','MT','WY','CO',
             'NM','TX','OK','KS','NE','SD','ND','MN','IA','MO',
             'AR','LA','MS','AL','TN','IL','WI','MI','IN','KY','OH',
             'WV','PA','NY','VT','ME','NH','MA','RI','CT','NJ','DE',
             'DC','MD','VA','NC','SC','GA','FL',
            ]

#Let us define the corresponding Latitudes and Longitudes for the capital cities of these states 
coords = [
    [47.042418,-122.893077],[44.931109,-123.029159],
    [38.555605,-121.468926],[39.160949,-119.753877],
    [33.448457,-112.073844],[40.7547,-111.892622],
    [43.613739,-116.237651],[46.595805,-112.027031],
    [41.145548,-104.802042],[39.7391667,-104.984167],
    [35.667231,-105.964575],[30.266667,-97.75],
    [35.482309,-97.534994],[39.04,-95.69],
    [40.809868,-96.675345],[44.367966,-100.336378],
    [46.8083,-100.7837],[44.95,-93.094],
    [41.590939,-93.620866],[38.572954,-92.189283],
    [34.736009,-92.331122],[30.4515, -91.1871],
    [32.320,-90.207],[32.361538,-86.279118],
    [36.165,-86.784],[39.783250,-89.650373],
    [43.074722,-89.384444],[42.7335,-84.5467],
    [39.790942,-86.147685],[38.197274,-84.86311],
    [39.962245,-83.000647],[38.349497,-81.633294],
    [40.269789,-76.875613],[42.659829,-73.781339],
    [44.26639,-72.57194], [44.323535,-69.765261],
    [43.220093,-71.549127],[42.2352,-71.0275],
    [41.8240,-71.4128],[41.767,-72.677],
    [40.221741,-74.756138],[39.161921,-75.526755],
    [38.972945,-76.501157],[38.913,-77.013],
    [37.54,-77.46],[35.771,-78.638],
    [34.000,-81.035],[33.76,-84.39],
    [30.4518,-84.27277],
]

Capitals = ['Olympia','Salem','Sacramento','Carson City','Phoenix','Salt Lake City','Boise','Helena',
            'Cheyenne','Denver','Santa Fe','Austin','Oklahoma City','Topeka','Lincoln','Pierre',
            'Bismarck','Saint Paul','Des Moines','Jefferson City','Little Rock','Baton Rouge','Jackson','Montgomery',
            'Nashville','Springfield','Madison','Lansing','Indianapolis','Frankfort','Columbus','Charleston',
            'Harrisburg','Albany','Montpelier','Augusta','Concord','Boston','Providence','Hartford',
            'Trenton','Dover','Washington D.C','Annapolis','Richmond','Raleigh','Columbia','Atlanta','Tallahassee',
]
Population = [46478,174365,513624,55916,1680992,200567,228959,32315,64235,727211,
              84683,978908,655057,125310,289102,13646,73529,308096,214237,42838,
              197312,220236,160628,198525,670820,114230,259680,118210,876384,27679,
              898553,46536,49528,96460,7855,18681,43627,692600,179883,122105,83203,
              38079,39174,230436,474069,131674,506811,194500,32113,345064,
]
tiles = ['stamenwatercolor', 'cartodbpositron', 'openstreetmap', 'stamenterrain','stamentoner']
for tile in tiles:
    folium.TileLayer(tile).add_to(ContinentalUSTourMap)
#feature_group = FeatureGroup(name='Some icons')    
folium.LayerControl().add_to(ContinentalUSTourMap)
#Let us iterate and add the markers for the capital cities of each state we are traversing
iterlength = len(coords)
iteratorno = 0
# iconc = folium.Icon(color="red",icon="fa-car", prefix='car',icon_size=(20, 20))
# iconc = folium.Icon(color="yellow",icon="fa-car", prefix='car',icon_size=(20, 20))
iconc = folium.Icon(color="purple",icon="fa-car", prefix='car',icon_size=(20, 20))

while iteratorno < iterlength:
    my_string = ' 2019 Population  : :<b> {} </b> <br>'.format(Population[iteratorno])
    my_string = my_string + ' State  : :<b> {} </b> <br>'.format(Locations[iteratorno])
    tootip_str = " Capital City <b>" + Capitals[iteratorno] + "</b><br>" + my_string
    ContinentalUSTourMap.add_child(
        folium.Marker(
            location=[coords[iteratorno][0], coords[iteratorno][1]],
            popup=my_string,
            # icon=DivIcon(icon_size=(50,50),html='<div style="font-size: 6pt; color : blue">' + str(iteratorno+1) + '</div>',),
            icon=DivIcon(icon_size=(50,50),html='<div style="font-size: 6pt; color : yellow">' + str(iteratorno+1) + '</div>',),
            tooltip = tootip_str,
            # radius=10+Population[iteratorno]/100000, 
             radius=10+Population[iteratorno]/10000, 
            color='blue',
            #  color='black',
            fill=True,
            # fill_color='yellow',
            fill_color='green',
            fill_opacity=0.2,
        )
    )
    ContinentalUSTourMap.add_child(
           folium.CircleMarker(
            location=[coords[iteratorno][0], coords[iteratorno][1]],
            radius=10+Population[iteratorno]/100000, 
            color='black',
            fill=True,
            fill_color='yellow',
            fill_opacity=0.2,
        )
    )
    iteratorno = iteratorno + 1
 
#feature_group.add_to(ContinentalUSTourMap)
LayerControl().add_to(ContinentalUSTourMap)
plugins.PolyLineOffset(
    coords, weight=2, dash_array="5,10", color="red", opacity=1
).add_to(ContinentalUSTourMap)
# plugins.PolyLineOffset(coords, color="#00f", opacity=1, offset=-3).add_to(ContinentalUSTourMap)
# plugins.PolyLineOffset(coords, color="#080", opacity=1, offset=3).add_to(ContinentalUSTourMap)

plugins.PolyLineOffset(coords, color="yellow", opacity=1, offset=-3).add_to(ContinentalUSTourMap)
plugins.PolyLineOffset(coords, color="green", opacity=1, offset=3).add_to(ContinentalUSTourMap)

ContinentalUSTourMap.get_root().html.add_child(folium.Element(title_html))
ContinentalUSTourMap.save("openstreetMap_change_color.html")
ContinentalUSTourMap